package com.algorithm.ch2.cjm.huisu;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jieming.chen on 2020/7/8
 */
public class LetterCombinations {

    public static void main(String[] args) {
        LetterCombinations combinations = new LetterCombinations();
        combinations.letterCombinations("23");
    }

    List<String> result = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return result;
        }
        StringBuilder nowStr = new StringBuilder();
        addNum(digits.toCharArray(), 0, nowStr);
        return result;
    }

    private char[] numToLetter(char num) {
        switch (num) {
            case '2':
                return new char[]{'a', 'b', 'c'};
            case '3':
                return new char[]{'d', 'e', 'f'};
            case '4':
                return new char[]{'g', 'h', 'i'};
            case '5':
                return new char[]{'j', 'k', 'l'};
            case '6':
                return new char[]{'m', 'n', 'o'};
            case '7':
                return new char[]{'p', 'q', 'r', 's'};
            case '8':
                return new char[]{'t', 'u', 'v'};
            case '9':
                return new char[]{'w', 'x', 'y', 'z'};
            default:
                return new char[0];
        }
    }

    private void addNum(char[] digits, int key, StringBuilder nowStr) {
        if (key == digits.length) {
            result.add(nowStr.toString());
            return;
        }
        char[] values = numToLetter(digits[key]);
        for (char c : values) {
            nowStr.append(c);
            addNum(digits, key + 1, nowStr);
            nowStr.deleteCharAt(key);
        }
    }
}
